<?php
/**
 * DictionaryController
 * 
 * @package cha
 * @author blog.anchen8.net
 * @copyright 2014
 * @version $Id$
 * @access public
 */
class DictionaryController extends AdminController 
{
	public $pageSize=100;
	//数据字典列表
	public function actionIndex(){
	    //Yii::app()->dict
        //echo phpinfo();exit;
        /*
        echo Yii::app()->dict->select('global','sex',1,array('name'=>'UserLife[schedule]' ,'empty'=>array(0=>'请选择')));
        exit;
        */
	    $CDbCriteria = new CDbCriteria();
	    if(isset($_REQUEST['search'])){
	        $CDbCriteria->compare('name', $_REQUEST['search'],true);
	        $_GET['search']=$_REQUEST['search'];
	    }
	    $CDbCriteria->order = '`sort` desc,did desc';
	    $count=BaseDictionaryModel::model()->count($CDbCriteria);
	    $pages=new CPagination($count);
	     
	    $pages->pageSize=$this->pageSize;
	    $pages->applyLimit($CDbCriteria);
	    $model = BaseDictionaryModel::model()->findAll($CDbCriteria);
	    
	    $this->render('index',array('model'=>$model,'pages'=>$pages));
	}
	//添加数据字典
	public function actionCreate(){
	    $model = new BaseDictionaryModel();
	    if(isset($_POST['ajax']) && $_POST['ajax']==='dictionary-_form-form')
	    {
	        echo CActiveForm::validate($model);
	        Yii::app()->end();
	    }
	    if(isset($_POST['BaseDictionaryModel'])){
	        $model->attributes=$_POST['BaseDictionaryModel'];
	        if($model->save()){
	            $alert="SendWebMsg('success','数据字典添加成功','".$this->createUrl('dictionary/index')."')";
	        }else{
	            if($model->hasErrors()){
	                $alert="SendWebMsg('cue','请修正页面错误提示项')";
	            }else{
	                $alert="SendWebMsg('error','你的网络不给力，操作失败')";
	            }
	        }
	        Yii::app()->getClientScript()->registerScript('msg',$alert,CClientScript::POS_END);
	    }
	    $this->render('_form',array('model'=>$model));
	}
	//编辑数据字典
	public function actionUpdate($did){
	    $model = BaseDictionaryModel::model()->findByPk($did);
	    if(isset($_POST['ajax']) && $_POST['ajax']==='dictionary-_form-form')
	    {
	        echo CActiveForm::validate($model);
	        Yii::app()->end();
	    }
	    if(isset($_POST['BaseDictionaryModel'])){
	        $model->attributes=$_POST['BaseDictionaryModel'];
	        if($model->save()){
	            $alert="SendWebMsg('success','数据字典更新成功','".$this->createUrl('dictionary/index')."')";
	        }else{
	            if($model->hasErrors()){
	                $alert="SendWebMsg('cue','请修正页面错误提示项')";
	            }else{
	                $alert="SendWebMsg('error','你的网络不给力，操作失败')";
	            }
	        }
            //$this->redirect($this->createUrl('dictionary/index'));
	        Yii::app()->getClientScript()->registerScript('msg',$alert,CClientScript::POS_END);
	    }
	    
	    $this->render('_form',array('model'=>$model));
	}
	//删除数据字典
	public function actionDelete($did){
	    $result = BaseDictionaryModel::model()->findByPk($did);        
	    if($result && $result->delete()){
			$this->ajaxReturn(0,'删除成功');
		}else{
			$this->ajaxReturn(1,'删除失败');
		}
	}
	//批量删除数据字典
	public function actionDeleteall(){
	    $check=1;
	    $ids=explode('|',$_POST['ids']);
        //$ids = array(58);
	    foreach ((array)$ids as $v){
	        $result=BaseDictionaryModel::model()->findByPk($v);
	        if($result && $result->delete()){
	
	        }else{
	            $check=0;
	        }
	    }
	    if($check){
	        $this->ajaxReturn(0,'删除成功');
	    }else{
	        $this->ajaxReturn(1,'删除失败');
	    }
	}
	//选项管理
	public function actionIndexoption($did){
	    $dict = BaseDictionaryModel::model()->findByPk($did);
	    $CDbCriteria = new CDbCriteria();
	    $CDbCriteria->condition = 'did=:did';
	    $CDbCriteria->params=array(':did'=>$did);
	    $CDbCriteria->order = '`sort` desc';
	    $count = BaseDictionaryOptionModel::model()->count($CDbCriteria);
	    $pages=new CPagination($count);
	    
	    $pages->pageSize=$this->pageSize;
	    $pages->applyLimit($CDbCriteria);
	    $model = BaseDictionaryOptionModel::model()->findAll($CDbCriteria);
	     
	    $this->render('indexoption',array('model'=>$model,'pages'=>$pages,'dict'=>$dict));
	}
	//添加数据字典项目
	public function actionAddoption($did){
	    $dict = BaseDictionaryModel::model()->findByPk($did);
	    $model = new BaseDictionaryOptionModel();
	    if(isset($_POST['ajax']) && $_POST['ajax']==='dictionary-_form-form')
	    {
	        echo CActiveForm::validate($model);
	        Yii::app()->end();
	    }
	    if(isset($_POST['BaseDictionaryOptionModel'])){
	        $model->attributes=$_POST['BaseDictionaryOptionModel'];
	        //$model->did=$did;
	        if($model->save()){
	            $alert="SendWebMsg('success','添加成功','".$this->createUrl('dictionary/indexoption',array('did'=>$did))."')";
	        }else{
	            if($model->hasErrors()){
	                $alert="SendWebMsg('cue','请修正页面错误提示项')";
	            }else{
	                $alert="SendWebMsg('error','你的网络不给力，操作失败')";
	            }
	        }
	        Yii::app()->getClientScript()->registerScript('msg',$alert,CClientScript::POS_END);
	    }
	    $this->render('_optionform',array('model'=>$model,'dict'=>$dict ,'did'=>$did));
	}
	//修改数据字典选项
	public function actionUpdateoption($id){
	    $model = BaseDictionaryOptionModel::model()->findByPk($id);
	    $dict = BaseDictionaryModel::model()->findByPk($model->did);
	    if(isset($_POST['ajax']) && $_POST['ajax']==='dictionary-_form-form')
	    {
	        echo CActiveForm::validate($model);
	        Yii::app()->end();
	    }
	    if(isset($_POST['BaseDictionaryOptionModel'])){
	        $model->attributes=$_POST['BaseDictionaryOptionModel'];
	        if($model->save()){
	            $alert="SendWebMsg('success','更新成功','".$this->createUrl('dictionary/indexoption',array('did'=>$model->did))."')";
	        }else{
	            if($model->hasErrors()){
	                $alert="SendWebMsg('cue','请修正页面错误提示项')";
	            }else{
	                $alert="SendWebMsg('error','你的网络不给力，操作失败')";
	            }
	        }
	        Yii::app()->getClientScript()->registerScript('msg',$alert,CClientScript::POS_END);
	    }
	    $this->render('_optionform',array('model'=>$model,'dict'=>$dict ,'did'=>$model->did));
	}
	//删除数据字典选项
	public function actionDeleteoption($id){
	    $result = BaseDictionaryOptionModel::model()->findByPk($id);
	    if($result && $result->delete()){
	        $this->ajaxReturn(0,'删除成功');
	    }else{
	        $this->ajaxReturn(1,'删除失败');
	    }
	}
	//批量删除数据字典选项
	public function actionDeleteoptionall(){
	    $check=1;
	    $ids=explode('|',$_POST['ids']);
	    foreach ((array)$ids as $v){
	        $result = BaseDictionaryOptionModel::model()->findByPk($v);
	        if($result && $result->delete()){
	
	        }else{
	            $check=0;
	        }
	    }
	    if($check){
	        $this->ajaxReturn(0,'删除成功');
	    }else{
	        $this->ajaxReturn(1,'删除失败');
	    }
	}
}
?>